<?php


namespace app\admin\controller;

use app\common\model\UserFeedbackModel;
use think\facade\Request;

/**
 * 反馈列表
 * Class FeedbackController
 * @date 2022-04
 */
class FeedbackController extends BaseController {

    public function index() {
        return $this->fetch();
    }


    public function getList() {
        $user_id    = Request::param("user_id");
        $start_time = Request::param("start_time");
        $end_time   = Request::param("end_time");

        // 排序
        $sort_field = Request::param("field", '', 'trim');
        $sort_order = Request::param("order", '', 'trim');

        // 分页
        $page  = Request::param("page");
        $limit = Request::param("limit");

        $where = [];
        if ($user_id > 0) {
            $where[] = ["uid", "=", $user_id];
        }
        if ($start_time) {
            $where[] = ["created_at", ">=", $start_time];
        }
        if ($end_time) {
            $where[] = ["created_at", "<", $end_time];
        }

        $order = "id desc";
        if (!empty($sort_field) && !empty($sort_order)) {
            $order = sprintf("%s %s", $sort_field, $sort_order);
        }

        $count = UserFeedbackModel::where($where)->count("id");
        if ($count > 0) {
            // 角色id
            $admin_roleid = $this->userinfo->roleid;

            $list = UserFeedbackModel::where($where)->with(['user' => function ($query) {
                $query->field('id,account');
            }])->order($order)
                ->limit(($page - 1) * $limit, $limit)
                ->select()
                ->each(function($query) use($admin_roleid){
                    if($query->user){
                        $query->user->account = mobileHide($query->user->account,$admin_roleid);
                    }
                });

            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }

        return self::bulidFail('暂无数据');
    }

    /**
     * 删除信息
     * @return \think\response\Json
     */
    public function delInfo() {
        $id             = Request::param('id', 0, 'intval');
        $feedback_model = UserFeedbackModel::get($id);
        if (!$feedback_model) {
            return self::bulidFail('未查询到相关反馈');

        }

        if (UserFeedbackModel::destroy($id)) {
            // 记录日志
            $log_content = "删除反馈，ID：{$feedback_model->id}，标题：{$feedback_model->title}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        }
        return self::bulidFail();

    }
}
